/*
 * @Author: your name
 * @Date: 2021-10-13 19:33:15
 * @LastEditTime: 2021-10-13 20:00:09
 * @LastEditors: Please set LastEditors
 * @Description: 控制自己的时间 坚定自己的本心
 * @FilePath: \luogu\动态规划\【动态规划1】动态规划的引入\P1616 疯狂的采药\main.cpp
 */
#include<iostream>
using namespace std;
#include<algorithm>

const int MAX = 10010; 
const int VMAX = 10000010; //10000010

int t[MAX],v[MAX];
long long int f[VMAX];

int main(){
    int T,M;
    cin >> T >> M;
    for(int i=1; i<=M; i++){
        cin >> t[i] >> v[i];
        //正序
        for(int j=t[i]; j<=T; j++){
            //获取 f[j] f[j-t[i]] + v[i]
            //获取f[j]时,其实f[j-t[i]]里也可能已经装载了i物品
            f[j] = max(f[j],f[j-t[i]]+v[i]);
        }
    }
    cout << f[T] << endl;
}